<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><TITLE>tailcall manual page - Tcl Built-In Commands</TITLE>
<link rel="stylesheet" href="../docs.css" type="text/css" media="all">
</HEAD>
<BODY><H2><a href="../contents.htm">Tcl8.6.11/Tk8.6.11 Documentation</a> <small>&gt;</small> <a href="contents.htm">Tcl Commands</a> <small>&gt;</small> tailcall</H2>
<H3><A HREF="../UserCmd/contents.htm">Tcl/Tk Applications</A> | <A HREF="../TclCmd/contents.htm">Tcl Commands</A> | <A HREF="../TkCmd/contents.htm">Tk Commands</A> | <A HREF="../ItclCmd/contents.htm">[incr Tcl] Package Commands</A> | <A HREF="../SqliteCmd/contents.htm">SQLite3 Package Commands</A> | <A HREF="../TdbcCmd/contents.htm">TDBC Package Commands</A> | <A HREF="../TdbcmysqlCmd/contents.htm">tdbc::mysql Package Commands</A> | <A HREF="../TdbcodbcCmd/contents.htm">tdbc::odbc Package Commands</A> | <A HREF="../TdbcpostgresCmd/contents.htm">tdbc::postgres Package Commands</A> | <A HREF="../TdbcsqliteCmd/contents.htm">tdbc::sqlite3 Package Commands</A> | <A HREF="../ThreadCmd/contents.htm">Thread Package Commands</A> | <A HREF="../TclLib/contents.htm">Tcl C API</A> | <A HREF="../TkLib/contents.htm">Tk C API</A> | <A HREF="../ItclLib/contents.htm">[incr Tcl] Package C API</A> | <A HREF="../TdbcLib/contents.htm">TDBC Package C API</A></H3>
<H3><A NAME="M2">NAME</A></H3>
tailcall &mdash; Replace the current procedure with another command
<H3><A NAME="M3">SYNOPSIS</A></H3>
<B>tailcall </B><I>command</I> ?<I>arg ...</I>?<BR>
<H3><A NAME="M4">DESCRIPTION</A></H3>
The <B>tailcall</B> command replaces the currently executing procedure, lambda
application, or method with another command. The <I>command</I>, which will
have <I>arg ...</I> passed as arguments if they are supplied, will be looked up
in the current namespace context, not in the caller's. Apart from that
difference in resolution, it is equivalent to:
<P>
<PRE>return [uplevel 1 [list <I>command</I> ?<I>arg ...</I>?]]</PRE>
<P>
This command may not be invoked from within an <B><A HREF="../TclCmd/uplevel.htm">uplevel</A></B> into a procedure
or inside a <B><A HREF="../TclCmd/catch.htm">catch</A></B> inside a procedure or lambda.
<H3><A NAME="M5">EXAMPLE</A></H3>
Compute the factorial of a number.
<P>
<PRE>proc factorial {n {accum 1}} {
    if {$n &lt; 2} {
        return $accum
    }
    <B>tailcall</B> factorial [expr {$n - 1}] [expr {$accum * $n}]
}</PRE>
<P>
Print the elements of a list with alternating lines having different
indentations.
<P>
<PRE>proc printList {theList} {
    if {[llength $theList]} {
        puts &quot;&gt; [lindex $theList 0]&quot;
        <B>tailcall</B> printList2 [lrange $theList 1 end]
    }
}
proc printList2 {theList} {
    if {[llength $theList]} {
        puts &quot;&lt; [lindex $theList 0]&quot;
        <B>tailcall</B> printList [lrange $theList 1 end]
    }
}</PRE>
<H3><A NAME="M6">SEE ALSO</A></H3>
<B><A HREF="../TclCmd/apply.htm">apply</A></B>, <B><A HREF="../TclCmd/proc.htm">proc</A></B>, <B><A HREF="../TclCmd/uplevel.htm">uplevel</A></B>
<H3><A NAME="M7">KEYWORDS</A></H3>
<A href="../Keywords/C.htm#call">call</A>, <A href="../Keywords/R.htm#recursion">recursion</A>, <A href="../Keywords/T.htm#tail recursion">tail recursion</A>
<div class="copy">Copyright &copy; 1993 The Regents of the University of California.
<BR>Copyright &copy; 1994-1996 Sun Microsystems, Inc.
</div>
</BODY></HTML>
